﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FifaChamps.API.Data;

namespace FifaChamps.API.Logic
{
    public class Player
    {
        public int ID { get; private set; }
        public string Name { get; private set; }
        public string Surname { get; private set; }
        public string Email { get; private set; }
        public string Phone { get; private set; }
        public string Nickname {get; private set;}
        public OnlineType? Online
        {
            get
            {
                var onlinetype = OnlineType.None;
                switch (m_online)
                {
                    case "psn":
                        onlinetype = OnlineType.PS3;
                        break;
                    case "xbox":
                        onlinetype = OnlineType.Xbox;
                        break;
                    case "pc":
                        onlinetype = OnlineType.PC;
                        break;
                    default:
                        onlinetype = OnlineType.None;
                        break;
                }
                return onlinetype;
            }
        }
        public Team FirstTeam { get; private set; }
        public Team SecondTeam { get; private set; }
        public DateTime? JoinDate { get; private set; }
        public bool? Registered { get; private set; }

        private string m_online;

        protected Player()
        { 
        }

        private Player(int playerid, string name, string surname, string email, string phone, string nickname, string online, Team firstteam, Team secondteam, DateTime? joindate, bool? registered)
        {
            ID = playerid;
            Name = name;
            Surname = surname;
            Email = email;
            Phone = phone;
            Nickname = nickname;
            m_online = online;
            FirstTeam = firstteam;
            SecondTeam = secondteam;
            JoinDate = joindate;
            Registered = registered;
        }

        public int Save()
        {
            if (this.ID != 0)
            {
                return Player.Update_Player(this);
            }
            return Player.Add_Player(this);
        }

        public static Player Factory(Data.Player player)
        {
            return Factory(player.PlayerID, player.Name, player.Surname, player.Email, player.Phone, player.NickName, player.Online, player.FirstTeam, player.SecondTeam, player.Picture, player.Joindate, player.Registered);
        }

        public static Player Factory(string name, string surname, string email, string phone, string nickname, string online, int? firstteam, int? secondteam, string playerpicture, DateTime? joindate, bool? registered)
        {
            return Factory(0, name, surname, email, phone, nickname, online, default(int?), default(int?), playerpicture, joindate, registered);
        }

        public static Player Factory(int playerid, string name, string surname, string email, string phone, string nickname, string online, int? firstteam, int? secondteam, string playerpicture, DateTime? joindate, bool? registered)
        {
            return new Player(playerid, name, surname, email, phone, nickname, online, null, null, joindate, registered);
        }

        public static List<Player> Get_Players()
        {
            List<Player> players = new List<Player>();
            DataManager.Get_Players().ForEach(data_player => players.Add(Player.Factory(data_player)));
            return players;
        }

        public static Player Get_Player(int playerID)
        {
            var player = DataManager.Get_Player(playerID);
            return Player.Factory(player.PlayerID, player.Name, player.Surname, player.Email, player.Phone,
                player.NickName, player.Online, player.FirstTeam, player.SecondTeam, player.Picture, player.Joindate, player.Registered);
        }

        /// <summary>
        /// Adds an instance of the player
        /// </summary>
        /// <param name="player"></param>
        /// <returns></returns>
        public static int Add_Player(Player player)
        {
            return DataManager.Add_Player(player.Name, player.Surname, player.Email, player.Phone, player.Nickname, player.m_online, default(int?), default(int?), null, player.JoinDate, player.Registered);
        }
        
        /// <summary>
        /// Updates a given player
        /// </summary>
        /// <param name="player"></param>
        /// <returns></returns>
        public static int Update_Player(Player player)
        {
            if (player.ID == 0) return 0;

            return DataManager.Update_Player(player.ID, player.Name, player.Surname, player.Email, player.Phone, player.Nickname, player.m_online, default(int?), default(int?), null);
        }
        
    }
}
